package terra;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;

import terra.network.Client;
import terra.slick.GameState;
import terra.slick.MenuState;
import terra.twl.TWLStateBasedGame;


public class Game extends TWLStateBasedGame {

	public Game() {
		super("Infinite");
		instance = this;
	}
	
	public static Game instance;
	
	@Override
	public boolean closeRequested() {
		Client.getInstance().bootstrap.shutdown();
		return true;
	}
	
	
	@Override
	public void initStatesList(GameContainer arg0){
		addState(MenuState.getInstance());
		addState(GameState.getInstance());
	}
	public static GameContainer app;
	
	public static void create() throws SlickException
	{
		app = new AppGameContainer(instance = new Game());
		((AppGameContainer) app).setDisplayMode(800, 600, false);
		app.setTargetFrameRate(60);
		app.setShowFPS(false);
		app.setAlwaysRender(true);
		app.setUpdateOnlyWhenVisible(true);
		((AppGameContainer) app).start();
	}
	
	static void prepareLog() {
		File log = new File("log.txt");
		if (!log.exists())
			try {
				log.createNewFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		PrintStream stream = null;
		try {
			stream = new PrintStream(log);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.setOut(stream);
		System.setErr(stream);
	}


	@Override
	protected URL getThemeURL() {
		return Loader.class.getResource("graphics/gui/theme.xml");
	}
	

}
